package com.cuit.qx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.qx.bean.Threshold;
import com.cuit.qx.dao.ThresholdManagementMapper;
import com.cuit.qx.service.ThresholdManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author:wjd
 * @Description: 阈值管理Service
 * @date:Created in 10:11 2020/9/17
 * @Modified By:
 */
@Service
public class ThresholdManagementServiceImpl extends ServiceImpl<ThresholdManagementMapper, Threshold> implements ThresholdManagementService {

    @Autowired
    ThresholdManagementMapper managementMapper;

    public List<Threshold> getAllThresholdData(int pageNo, int pageSize) {
        IPage<Threshold> userPage = new Page<>(pageNo, pageSize);//参数一是当前页，参数二是每页个数

        userPage = managementMapper.selectPage(userPage, null);

        return userPage.getRecords();
    }

    @Override
    public int insertThreshold(Threshold threshold) {
        return managementMapper.insert(threshold);
    }

    @Override
    public int updateThresholdId(Threshold threshold) {
        return managementMapper.updateThresholdById(threshold);

    }

    @Override
    public int updateThresholdList(List<Threshold> thresholds) {
        return managementMapper.updateThresholdForeach(thresholds);
    }

    @Override
    public List<Threshold> getThreshold(Integer pageNo, Integer pageSize, String station, String stationName) {
        return managementMapper.getThresholdData(pageNo, pageSize, station, stationName);
    }

    @Override
    public int deleteThresholdById(String id) {
        return managementMapper.deleteThresholdById(id);
    }

    @Override
    public int deleteThresholdByIds(String ids) {
        String[] thresholdIds = ids.split(",");
        return managementMapper.deleteThresholdByIds(thresholdIds);

    }

    @Override
    public int getCount(String station, String stationName) {
        QueryWrapper<Threshold> operationQuery = new QueryWrapper<Threshold>();
        if (station != null) {
            operationQuery.eq("station", station);
        }
        if (stationName != null) {
            operationQuery.eq("station_name", stationName);
        }
        int size = managementMapper.selectCount(operationQuery);
        return size;
    }

    @Override
    public List<Threshold> getByState() {

        return managementMapper.getByState();
    }

    /**
     * 批量修改阈值要素值
     * @param ids
     * @param wind
     * @param tem
     * @param rain
     * @param press
     * @param hum
     * @return
     */
    @Override
    public Boolean updateBatchByElement(String ids, String wind, String tem, String rain, String press, String hum) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<Threshold> thresholdList = managementMapper.selectBatchIds(idList);
        for (Threshold threshold : thresholdList){
            if (wind!=null){
                threshold.setWind(wind);
            }
            if (tem!=null){
                threshold.setTemperature(tem);
            }
            if (rain!=null){
                threshold.setPrecipitation(rain);
            }
            if (press!=null){
                threshold.setPressure(press);
            }
            if (hum!=null){
                threshold.setHumidity(hum);
            }
        }

        int i = managementMapper.updateThresholdForeach(thresholdList);
        return i==1;
    }


}
